﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Leadtools.Codecs;

using MINERP.MODEL.StartOrder;

namespace MINERP.BLL.StartOrder
{
    public class PictrueBLL
    {
        #region 私有变量

        /// <summary>
        /// 加载保存图片的对象
        /// </summary>
        private  RasterCodecs codecs;

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public PictrueBLL()
        {
            codecs = new RasterCodecs();
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~PictrueBLL()  
        {
            codecs.Dispose();
        }

        /// <summary>
        /// 加载单个文件(不需要设计)
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <param name="codecs"></param>
        public PhotoInfo AddSinglePhotoNoDesign(string fileFullPath)
        {
            try
            {
                PhotoInfo  photo = AddPhotoCommon(fileFullPath);
                CodecsImageInfo imgInfo = codecs.GetInformation(fileFullPath, true);
                int resolution = imgInfo.XResolution;
                int xPixel = imgInfo.Width;
                int yPixel = imgInfo.Height;

                float standardWidth = (float)xPixel / resolution;
                float standardHeight = (float)yPixel / resolution;

                //照片分辨率
                photo.PhotoDPI = resolution;
                //照片标准高宽，用于计算
                photo.Width = standardWidth;
                photo.Height = standardHeight;
                //照片像素高、宽
                photo.PixWidth = xPixel;
                photo.PixHeight = yPixel;
                if (yPixel == 0 || xPixel == 0)
                {
                    throw new Exception("照片 " + fileFullPath + " 未能读取成功，请检查");

                }
                //高宽、面积需要按单位的不同作不同的显示

                return photo;
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// 加载照片的中间操作
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        private PhotoInfo AddPhotoCommon(string fileFullPath)
        {
            PhotoInfo photo = new PhotoInfo();
            FileInfo fileInfo = new FileInfo(fileFullPath);
            //照片全路径
            photo.PicPath = fileInfo.FullName;
            //照片名称
            photo.PhotoName = fileInfo.Name;
            //照片数量
            photo.PhotoNum = 1;
            //照片大小
            string sizeStr = "";
            long fileSize = fileInfo.Length;
            photo.PhotoLength = fileSize;           //在CRC校验中，取同一个产品中长度最长的照片比较
            if (fileSize < 1024)
            {
                sizeStr = fileSize.ToString() + "B";
            }
            else if (fileSize < 1024 * 1024)
            {
                sizeStr = string.Format("{0:0.##}", (double)fileSize / 1024) + "K";
            }
            else if (fileSize < 1024 * 1024 * 1024)
            {
                sizeStr = string.Format("{0:0.##}", (double)fileSize / 1024 / 1024) + "M";
            }
            else
            {
                sizeStr = string.Format("{0:0.##}", (double)fileSize / 1024 / 1024 / 1024) + "G";
            }

            //照片备注(默认为照片名)
            //int index1 = fileInfo.FullName.IndexOf("等待开单");
            //string strTempPath = fileInfo.FullName.Substring(index1);//从等等开单的临时的文路径

            //int index = strTempPath.LastIndexOf(".");
            //string[] patharry = strTempPath.Split('\\');
            //string strTemp = patharry[patharry.Length - 1];


            //for (int i = patharry.Length - 2; i > 2; i--)
            //{
            //    strTemp = patharry[i] + "\\" + strTemp;
            //}

            //photo.PhotoRemark = strTemp;
            return photo;
        }

    }
}
